<%@page language="java" pageEncoding="UTF-8" contentType="text/html;charset=utf-8"%>
<%@include file="/includes/taglibs.jsp"%>
<%@page import="com.feib.soeasy.util.UserUtil"%>
<%@page import="com.feib.soeasy.util.CodeTableUtil"%>
<%@page import="com.feib.soeasy.model.UploadFile"%>

<script type="text/javascript">
	<!--	
	$(document).ready(function() {	
		$("[id^='viewPaymentImg-']").click(function() {
			var paymentPk = this.id.substring(15);
			doQueryPaymentsAction(paymentPk);
			
		});
		
		$('#printBtn2').click(function() {
			//window.print();
			//doQueryPrintAction();
			divPrint('queryResultDiv');//只印目前這筆
		});
		
	});
	
	$("#checkAll").click(function() {
		doCheckAll();
	});
	
	$("#unCheckAll").click(function() {
		undoCheckAll();
	});
	
	function   showTable(obj){
		if(obj.value == "999"){			
			document.getElementById("paymentWriteOffReason").value = "";
			document.getElementById("paymentWriteOffReason").readOnly = false;
		}else if(obj.value == ""){			
			document.getElementById("paymentWriteOffReason").value = "";
			document.getElementById("paymentWriteOffReason").readOnly = true;
		}else{			
            var select = document.getElementById("paymentWriteOffCode");
            var cancelReason = select.options[select.selectedIndex].text;
            var reason = "";
            if(reason != null && cancelReason.split(" ").length > 1 )reason = cancelReason.split(" ")[1];
			document.getElementById("paymentWriteOffReason").value = reason;
			document.getElementById("paymentWriteOffReason").readOnly = true;
		}
	}
	
	$("[id^='doPaymentWriteOffBtn']").click(function() {
		if(document.getElementById( "paymentWriteOffCode").value == ""){
			var errMsg = "";
			errMsg = "銷帳原因是必選欄位<br/>"
				if (0 != errMsg.length){
					$("#ajaxErrorMessageDiv").html(errMsg);
					$("#ajaxErrorMessageDiv" ).dialog( "destroy" );
					$("#ajaxErrorMessageDiv" ).dialog({  
						title:'請修正下列錯誤' 
						,width: 300
						, resizable: false
						, modal: true
						, buttons: {
							Ok: function() {
								$( this ).dialog( "close" );
							}
						}
					});
					return false;
				}				 
		}
		doPaymentWriteOffConfirmAction();
	});
	
	//-->
	</script>
	
<c:choose>
	<c:when test="${null != actionBean.queryResult && not empty actionBean.queryResult.paginatedList}">
		<c:set var="lastPageNumber" value="${actionBean.queryResult.totalPageCount}"/>
		<c:set var="currentPageNumber" value="${actionBean.queryResult.pageNumber}"/>
		<c:set var="prePageNumber" value="${actionBean.queryResult.pageNumber - 1}"/>
		<c:set var="nextPageNumber" value="${actionBean.queryResult.pageNumber + 1}"/>
		<stripes:form name="confirmHiddenFrom" id="confirmHiddenFrom" beanclass="com.feib.soeasy.action.Soez1017ActionBean" 
		method="post" onsubmit="javascript: return false;">
			<table id="listTable">
				<thead>
					<tr>
						<th id="paginationBar" colspan="7">
							<input type="button" id="firstPageBtn" value="第一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
							<input type="button" id="prePageBtn" value="上一頁" <c:out value='${prePageNumber<=0 ? "disabled": "" }'/> />
							第 <input type="text" id="currentPageText" value="${currentPageNumber }" size="3" maxlength="4"> / ${lastPageNumber} 頁
							<input type="button" id="nextPageBtn" value="下一頁" <c:out value='${lastPageNumber<nextPageNumber ? "disabled": "" }'/> />
							<input type="button" id="lastPageBtn" value="最末頁" <c:out value='${lastPageNumber<=currentPageNumber ? "disabled": "" }'/> />
						</th>
					</tr>
					<tr>
						<th></th>
						<th>繳費帳號</th>
						<th>客戶編號</th>
						<th>姓名</th>
						<th>繳費金額</th>
						<th>繳費期限</th>
						<th>繳費狀態</th>
						<th>&nbsp;</th>
					</tr>
				</thead>
				<tbody>
					<c:forEach items="${actionBean.queryResult.paginatedList}" var="viewObj" varStatus="rowstat">
					<tr>
						<td align="center">
						<input name="checkPayment[${rowstat.index}].paymentPk" 
							type="checkbox" 
							id="checkPayment[${rowstat.index}].paymentPk" 
							value="<c:out value='${viewObj.paymentPk}'/>" class="chk"/>
						</td>
						<td><c:out value="${viewObj.virtualAccount}"/></td>
						<td><c:out value="${viewObj.payerId}"/></td>
						<td><c:out value="${viewObj.payerName}"/></td>
						<td align="right"><c:out value="${viewObj.totalAmount}"/></td>
						<td align="center"><fmt:formatDate value="${viewObj.payDue}" pattern="yyyy/MM/dd" /></td>
						<td>待繳費</td>
						<td>
							<div id="viewPaymentImg-<c:out value='${viewObj.paymentPk}'/>"><a href="javascript: void(0)" class="btnZoom">查詢</a></div>
						</td>				
					</tr>
					</c:forEach>
				</tbody>
			</table>			
			<input type="hidden" id="method" name="doAjaxPaymentWriteOff"/>
			<stripes:hidden id="hiddened.qryPayment.paymentStatus" name="qryPayment.paymentStatus"/>
			<stripes:hidden id="hiddened.group.groupNo" name="group.groupNo" />	
				<div id="paginationBar">
					<input type="button" id="checkAll" name="checkAll" value="全選" />
					<input type="button" id="unCheckAll" name="unCheckAll" value="全不選" />
					銷帳代碼<span id="mustKeyInStar">**</span>
					<stripes:select id="paymentWriteOffCode" name="paymentWriteOffCode" onchange="javascript:showTable(this)">
							<stripes:option value="" label="--請選擇--" />
						<c:forEach items="${actionBean.selectedPaymentWriteOffCode}" var="selectedPaymentWriteOffCodeObj" varStatus="rowstat">
							<stripes:option value="${selectedPaymentWriteOffCodeObj.value}" label="${selectedPaymentWriteOffCodeObj.label}" />
						</c:forEach>
							<stripes:option value="999" label="999  其他" />
					</stripes:select>			
					銷帳原因<stripes:text id="paymentWriteOffReason" name="paymentWriteOffReason" readonly="true" size="25" maxlength="200" />
					<input type="button" id="doPaymentWriteOffBtn" name="doPaymentWriteOffBtn" value="核銷" />
					<input type="button" id="printBtn2" value="列印" />		
				</div>
		</stripes:form>	
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$("[id^='firstPageBtn']").click(function() {
				doQueryAction(1);
			});
			
			$("[id^='prePageBtn']").click(function() {
				doQueryAction(<c:out value='${prePageNumber}'/>);
			});
			
			$("[id^='nextPageBtn']").click(function() {
				doQueryAction(<c:out value='${nextPageNumber}'/>);
			});
			
			$("[id^='lastPageBtn']").click(function() {
				doQueryAction(<c:out value='${lastPageNumber}'/>);
			});
			
			$("[id^='currentPageText']").change(function() {
				var value = this.value;
				if (value != ''){
					value = value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
					var intRegex = /^\d+$/;
					if(!intRegex.test(value)) {
						alert('頁數必須是整數');
						return ;
					}
					
					value = parseInt(value, 10);
					
					if (value < 1){
						alert('頁數必須是大於1');
						return ;
					}
					else if (value > <c:out value='${lastPageNumber}'/>){
						alert('頁數必須是小於總頁數 (<c:out value='${lastPageNumber}'/>)');
						return ;
					}
					else if (value != <c:out value='${currentPageNumber}'/>){
						doQueryAction(value);
					}
					
				}
				else{
					alert('頁數不可為空白');
					return ;
				}
			});
			
		});
		//-->
		</script>
			
	</c:when>
	<c:otherwise>
		<div id="queryNothingBlockDiv" style="display: none;">
			查無資料
		</div>		
		<script type="text/javascript">
		<!--
		$(document).ready(function() {
			$( "#queryNothingBlockDiv" ).dialog( "destroy" );
			$( "#queryNothingBlockDiv" ).dialog({  title:'<%=UserUtil.getCurrentMenuItemName() %>查詢' ,width: 300, resizable: false, modal: true, buttons : {
				Ok : function() {
					$(this).dialog("close");
				}
			}  });
		});
		//-->
		</script>
	</c:otherwise>
</c:choose>
